﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using DG.Tweening;

public class BirdFly : MonoBehaviour {

    public Transform target;
    public Transform standTarget;
    public Transform center;
    public Animator ani;
    private bool canRotate = false;
    private float time;
    private int flySpeed = 100;

	// Use this for initialization
	void Start ()
    {
        ani.SetTrigger("Fly");
        //transform.LookAt(target);
        transform.DOMove(target.position,1).SetEase(Ease.Linear).OnComplete(()=> 
        {
            StartCoroutine("FlyRotate");  
        });
	}
	
	IEnumerator FlyRotate()
    {
        transform.DOLocalRotate(new Vector3(0, -180, 0), 0.1f);
        yield return new WaitForSeconds(0.2f);
        canRotate = true;
        
    }

    private void Update()
    {
        if (canRotate==true)
        {
            transform.parent = center;
            center.Rotate(Vector3.up*flySpeed*Time.deltaTime);
            time +=Time.deltaTime;
            if (time>3)
            {
                StartCoroutine("FlyToPlayer");
                canRotate = false;
            }
        }
    }

    IEnumerator FlyToPlayer()
    {
        yield return new WaitForSeconds(0);
        transform.DOMove(standTarget.position,1).SetEase(Ease.Linear).OnComplete(()=> 
        {
            transform.DORotate(new Vector3(0,-90,0),0.1f);
            GameObject.Find("Role_XiaoQi").GetComponent<Animator>().SetTrigger("Happy");

            //分数
            GameObject.Find("UI_Game").SendMessage("AddNum", 7);
            //关卡解锁
            if (!LevelSingleton.GetInstance().levelData.ContainsKey(7))
            {
                LevelSingleton.GetInstance().levelData.Add(7, 1);
            }
            
            PlayerPrefs.SetInt("7", 1);
            Level7BeginEnd.Instance.EndGame();
        });

    }
}
